//
//  BaseTableViewController.m
// 

#import "BaseTableViewController.h"

@interface BaseTableViewController ()

@property (nonatomic) UIImageView *iconV;
@property (nonatomic) UILabel *tipsLab;
@property (nonatomic) UIButton *refreshBtn;

@end

@implementation BaseTableViewController


- (UIImageView *)iconV{
    if (!_iconV) {
        _iconV = [[UIImageView alloc]init];
    }
    return _iconV;
}

- (UILabel *)tipsLab{
    if (!_tipsLab) {
        _tipsLab = [Utility createLabel:[UIFont systemFontOfSize:14] color:[UIColor colorWithHexString:@"#999999"]];
        _tipsLab.textAlignment = 1;
    }
    return _tipsLab;
}

- (UIButton *)refreshBtn{
    if (_refreshBtn == nil) {
        _refreshBtn = [[UIButton alloc] init];
        [_refreshBtn addTarget:self action:@selector(clickRefresh) forControlEvents:UIControlEventTouchUpInside];
        _refreshBtn.titleLabel.font = [UIFont systemFontOfSize:16];
        [_refreshBtn setTitleColor:[UIColor colorWithHexString:@"#393F47"] forState:UIControlStateNormal];
    }
    return _refreshBtn;
}

- (void)clickRefresh{
    if (_callBackRefreshBlock) {
        _callBackRefreshBlock();
    }
}

-(NSMutableArray *)data{
    if(!_data){
        _data = [NSMutableArray array];
    }
    return _data;
}

- (void)isErrorData{
    if (self.data.count == 0) {
        self.tableView.scrollEnabled = NO;
        
        CGFloat widthHeight = 120;
        UIImage *img = [UIImage imageNamed:@"icon_errorData"];
        self.iconV.frame = CGRectMake((kScreenWidth - widthHeight)/2, (kScreenHeight - kNavigationBarHeight - kHeight_TabBar - widthHeight - 60)/2, widthHeight, widthHeight);
        self.iconV.image = img;
        
        self.tipsLab.frame = CGRectMake((kScreenWidth - 150)/2, self.iconV.bottom + 5, 150, 35);
        self.tipsLab.text = @"";
        
        self.refreshBtn.frame = CGRectMake(0, self.tipsLab.bottom + 5 , 180, 40);
        self.refreshBtn.centerX = self.tipsLab.centerX;
        
        [self.refreshBtn setEnabled:40];
        [self.view addSubview:self.iconV];
        [self.view addSubview:self.tipsLab];
        self.refreshBtn.layer.cornerRadius =20;
        self.refreshBtn.layer.masksToBounds = YES;
    }
    else{
        
        self.tableView.scrollEnabled = YES;
        
        [self.iconV removeFromSuperview];
        [self.tipsLab removeFromSuperview];
        [self.refreshBtn removeFromSuperview];
    }
}

- (void)endRefreshing{
}

- (void)isHaveEmptyData:(NSString *)str withEmptyImg:(NSString *)imgStr withDistance:(CGFloat)dis withOriginY:(CGFloat)y withBgColor:(UIColor *)color{
    [self.iconV removeFromSuperview];
    [self.tipsLab removeFromSuperview];
    [self.refreshBtn removeFromSuperview];
    if (self.data.count == 0) {
        self.tableView.disFloat = dis;
        self.tableView.floatY = y;
        self.tableView.startTip = YES;
        self.tableView.tipView.backgroundColor = color;
        self.tableView.tipImage = [UIImage imageNamed:@"icon_emptyData"];
        self.tableView.tipView.hidden = NO;
        self.tableView.tipTitle = @"there is no data";
        if (!PMStringIsEmpty(str)) {
            self.tableView.tipTitle = str;
        }
        if (!PMStringIsEmpty(imgStr)) {
            self.tableView.tipImage = [UIImage imageNamed:imgStr];
        }

    }else{
        self.tableView.tipView.hidden = YES;
        self.tableView.scrollEnabled = YES;
    }
}

- (void)isHaveEmptyData{
    [self.iconV removeFromSuperview];
    [self.tipsLab removeFromSuperview];
    [self.refreshBtn removeFromSuperview];
    self.tableView.tipView.hidden = YES;
    self.tableView.scrollEnabled = YES;
}

- (void)viewDidLoad{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self createTableView];
    
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        _tableView.estimatedSectionFooterHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
        _tableView.estimatedRowHeight = 0;
    } else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    
    if (@available(iOS 15.0, *)) {
        _tableView.sectionHeaderTopPadding = 0;
    }
    
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.backgroundColor = kTableViewBackColor;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
    _tableView.showsVerticalScrollIndicator = NO;
    
    [self.view addSubview:_tableView];
    
}

- (void)createTableView {
    
    if (_isTableViewStyleGrouped){
        _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    } else {
        _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    }
    
}


#pragma mark - tableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.data.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    return nil;
}

@end


@implementation CustomizedBaseTableViewController

- (void)createTableView {
    _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
}

@end

